package com.withoutwords.ui;

import java.util.ArrayList;

import kankan.wheel.widget.OnWheelChangedListener;
import kankan.wheel.widget.OnWheelScrollListener;
import kankan.wheel.widget.WheelView;
import kankan.wheel.widget.adapters.ArrayWheelAdapter;

import com.withoutwords.R;
import com.withoutwords.engine.GameController;
import com.withoutwords.engine.Round;
import com.withoutwords.engine.RoundMediaTime;
import com.withoutwords.engine.RoundMediaType;
import com.withoutwords.ui.custom.ImageWheelAdapter;

import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;

public class StartRoundActivity extends BaseActivity implements OnClickListener {

	Round round;
	int[][] icons = new int[][] {
			{ R.drawable.film_old, R.drawable.play_old, R.drawable.song_old,
					R.drawable.series_old },
			{ R.drawable.film_new, R.drawable.play_new, R.drawable.song_new,
					R.drawable.series_new },
			{ R.drawable.film_current, R.drawable.play_current,
					R.drawable.song_current, R.drawable.series_current } };
	// Scrolling flag
	private boolean scrolling = false;

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.startround);

		initViews();
		// game = GameController.getInstance().
		ArrayList<String> names = GameController.getInstance()
				.getCurrentGamePlayerNames();
		String strPlayerNames = "";
		for (String name : names) {
			strPlayerNames += name;
			strPlayerNames += " , ";
		}
		// to remove the last comma
		strPlayerNames = strPlayerNames.substring(0,
				strPlayerNames.length() - 3);

		TextView txtPlayers = (TextView) findViewById(R.id.txtPlayers);
		txtPlayers.setText(strPlayerNames);

		round = new Round();

	}

	void initViews() {

		Button btn = (Button) findViewById(R.id.btnStartRound);
		btn.setOnClickListener(this);

		final WheelView wheelType = (WheelView) findViewById(R.id.wheelViewType);
		ImageWheelAdapter imgAdpt = new ImageWheelAdapter(this);
		imgAdpt.setItems(icons[1]);

		wheelType.setViewAdapter(imgAdpt);
		wheelType.setCurrentItem(1);

		/*
		 * wheel.addChangingListener(changedListener);
		 * wheel.addScrollingListener(scrolledListener);
		 */
		wheelType.setCyclic(true);
		// wheel.setEnabled(false);

		final WheelView wheelTime = (WheelView) findViewById(R.id.wheelViewTime);
		ArrayWheelAdapter<String> adapter = new ArrayWheelAdapter<String>(
				this,
				new String[] { getString(R.string.old),
						getString(R.string.middle), getString(R.string.recent) });
		adapter.setTextSize(36);
		wheelTime.setViewAdapter(adapter);
		wheelTime.setCurrentItem(1);
		wheelTime.setCyclic(true);

		wheelTime.addChangingListener(new OnWheelChangedListener() {
			public void onChanged(WheelView wheel, int oldValue, int newValue) {
				if (!scrolling) {

					updateImageWheel(wheelType, newValue);
				}
			}
		});

		wheelTime.addScrollingListener(new OnWheelScrollListener() {
			public void onScrollingStarted(WheelView wheel) {
				scrolling = true;
			}

			public void onScrollingFinished(WheelView wheel) {
				scrolling = false;
				updateImageWheel(wheelType, wheelTime.getCurrentItem());
				// updateCities(wheelType, cities, country.getCurrentItem());
			}
		});
	}

	@Override
	public void onClick(View v) {
		switch (v.getId()) {
		case R.id.btnStartRound: {
			WheelView wheel = (WheelView) findViewById(R.id.wheelViewTime);
			round.setMediaTime(RoundMediaTime.values()[wheel.getCurrentItem()]);

			wheel = (WheelView) findViewById(R.id.wheelViewType);
			round.setMediaType(RoundMediaType.values()[wheel.getCurrentItem()]);

			EditText txt = (EditText) findViewById(R.id.editMediaName);
			round.setMediaName(txt.getText().toString());
			GameController.getInstance().createRound(round);

			break;
		}
		// case R
		}

	}

	/**
	 * Updates the city wheel
	 */
	private void updateImageWheel(WheelView imageWheel, int index) {
		ImageWheelAdapter imgAdpt = new ImageWheelAdapter(this);
		imgAdpt.setItems(icons[index]);
		// adapter.setTextSize(18);
		imageWheel.setViewAdapter(imgAdpt);
	//	imageWheel.setCurrentItem(icons[index].length / 2);
	}
	// Wheel scrolled flag
	/*
	 * private boolean wheelScrolled = false;
	 * 
	 * // Wheel scrolled listener OnWheelScrollListener scrolledListener = new
	 * OnWheelScrollListener() { public void onScrollingStarted(WheelView wheel)
	 * { wheelScrolled = true; } public void onScrollingFinished(WheelView
	 * wheel) { wheelScrolled = false; updateStatus(); } };
	 * 
	 * // Wheel changed listener private OnWheelChangedListener changedListener
	 * = new OnWheelChangedListener() { public void onChanged(WheelView wheel,
	 * int oldValue, int newValue) { if (!wheelScrolled) { updateStatus(); } }
	 * };
	 */

}
